import {
  pressures,
  joinPressure,
  createunifiedpressureorder
} from '../../api/index.js'

Page({
  data: {
    show: false,
    pressureId: '',
    pressure: '',
    level: 1,
    url: wx.getStorageSync('url'),
    click: false,
    days:null
  },

  onLoad: function(options) {
    this.setData({
      pressureId: options.pressureId
    })
    this.pressure(options.pressureId)
  },

  pressure(pressureId) {
    pressures(pressureId).then(res => {
      this.setData({
        pressure: res,
        days: res.days,
        level: res.level
      })
      console.log('pressure level:', this.data.level)
    })
  },

  onClickShow() {
    this.setData({
      show: true
    })
  },
  onClickHide() {
    if(this.data.click){
      wx.navigateTo({
        url: '/pages/schedule/schedule',
      })
    }
    this.setData({
      show: false
    });
  },


  handleJoinPressure() {
    let that=this
    let data = {
      pressureId: this.data.pressureId
    }
    joinPressure(data).then(res => {
      if (res.errcode == '00000') {
        let userPressureId = res.userPressureId
        if (res.price == 0) {
          wx.navigateTo({
            url: '/pages/pressure/pressure',
          })
        } else if (res.price > 0) {
          let data = {
            userPressureId: userPressureId
          }
          createunifiedpressureorder(data).then(res => {
            wx.requestPayment({
              timeStamp: res.timeStamp,
              nonceStr: res.nonceStr,
              package: res.package,
              signType: res.signType,
              paySign: res.paySign,
              success(res) {
                if (res.errMsg == 'requestPayment:ok') {
                  wx.navigateTo({
                    url: '/pages/pressure-schedule/pressure-schedule',
                  })
                } else {
                  wx.showToast({
                    title: '支付失败',
                    icon: 'none'
                  })
                }
              }
      
            })
          })
        }
      }
    })
  },

  //分享
  onShareAppMessage: function () {
    return {
      title: '我正在使用SMART睡眠训练计划，超有用！推荐给你',
      imageUrl: '../../images/98875.png'

    }
  },

})